package com.jinke.api.modules.app.config;

import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.read.metadata.ReadSheet;
import com.jinke.api.common.exception.Asserts;
import com.jinke.api.modules.app.controller.res.PriceSurchargeImportDto;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.compress.utils.Lists;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import java.math.RoundingMode;
import java.util.List;
import java.util.Map;

/**
 * @projectName:
 * @title: PriceSurchargeImportListener
 * @description: 自定义导入price surcharge监听器
 * @see:
 * @author: hhp
 * @version: 1.0.0
 * @createTime: 2024/7/30 11:38
 */
@Component
@Scope("prototype")
@Slf4j
public class PriceSurchargeImportListener extends AnalysisEventListener<PriceSurchargeImportDto.PriceSurchargeItem> {

    private PriceSurchargeImportDto.PriceSurchargeItem currentData;

    private int curIndex = 0;

    @Override
    public void invoke(PriceSurchargeImportDto.PriceSurchargeItem priceSurchargeItem, AnalysisContext analysisContext) {
        if (ObjectUtil.isEmpty(currentData) || (StrUtil.isNotBlank(priceSurchargeItem.getServiceCode()) && StrUtil.isNotBlank(priceSurchargeItem.getItem()))) {
            currentData = priceSurchargeItem;
        }
        if (ObjectUtil.isNotEmpty(currentData) && StrUtil.isNotBlank(currentData.getServiceCode())) {
            if (StrUtil.isBlank(priceSurchargeItem.getItem())) {
                priceSurchargeItem.setItem(currentData.getItem());
            }
            if (StrUtil.isBlank(priceSurchargeItem.getServiceCode())) {
                priceSurchargeItem.setServiceCode(currentData.getServiceCode());
            }
        }
        if (ObjectUtil.isNotNull(priceSurchargeItem.getRate())) {
            priceSurchargeItem.setRate(priceSurchargeItem.getRate().setScale(2, RoundingMode.UP));
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {

    }

    public void setCurIndex(int curIndex) {
        this.curIndex = curIndex;
    }

    @Override
    public void invokeHeadMap(Map<Integer, String> headMap, AnalysisContext context) {
        Map<Integer, String> integerStringMap = getPriceTemplateHeadMaps().get(curIndex);
        if (!areMapsEqualIgnoreCase(integerStringMap, headMap)) {
            Asserts.fail("上传文件内容格式错误, 请上传标准附加费表excel");
        }
        curIndex++;
    }

    private List<Map<Integer, String>> getPriceTemplateHeadMaps() {
        List<Map<Integer, String>> headMaps = Lists.newArrayList();
        Map<Integer, String> head1 = MapUtil.newHashMap();
        head1.put(0, "Surcharge");
        head1.put(1, null);
        head1.put(2, "zone");
        head1.put(3, "rate");
        head1.put(4, null);
        head1.put(5, null);
        head1.put(6, null);
        head1.put(7, null);
        headMaps.add(head1);
        return headMaps;
    }

    private static boolean areMapsEqualIgnoreCase(Map<Integer, String> map1, Map<Integer, String> map2) {
        if (map1.size()!= map2.size()) {
            return false;
        }

        for (Map.Entry<Integer, String> entry : map1.entrySet()) {
            Integer key = entry.getKey();
            String value1 = entry.getValue();
            String value2 = map2.get(key);

            if (!(StrUtil.isEmpty(value1) && StrUtil.isEmpty(value2))) {
                if (StrUtil.isEmpty(value2) ||!value1.equalsIgnoreCase(value2)) {
                    return false;
                }
            }
        }
        return true;
    }
}
